package com.dd.cloud.common.config.cookie;

import com.dd.cloud.common.utils.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * @author zk
 */
@Data
@Slf4j
@ConfigurationProperties(prefix = "dadao.jwt")
public class JwtProperties {
    private boolean enable;
    private String secret; // 密钥
    private String pubKeyPath;// 公钥
    private String priKeyPath;// 私钥
    private int expire;// token过期时间

    private PublicKey publicKey; // 公钥
    private PrivateKey privateKey; // 私钥

    // 对象一旦实例化后,就应该读取公钥和私钥
    // 构造函数执行完毕后就执行
    @PostConstruct
    public void init() {
        if (!enable) {
            return;
        }
        ClassLoader classLoader = this.getClass().getClassLoader();
        log.info("classLoader{},", classLoader);
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(pubKeyPath);
            System.out.println(inputStream);
            if (inputStream != null) {
                byte[] buffer = new byte[1024];
                int len;
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                while ((len = inputStream.read(buffer)) != -1) {
                    bos.write(buffer, 0, len);
                }
                bos.close();
                byte[] array = bos.toByteArray();
                log.info("获取公钥和私钥 read={}", array.length);
                if (array.length > 0) {
                    this.publicKey = RsaUtils.getPublicKey(array);
                } else {
                    throw new RuntimeException("JwtProperties init error");
                }
                inputStream.close();
            } else {
                throw new RuntimeException("JwtProperties init error");
            }
            //私钥
            inputStream = this.getClass().getClassLoader().getResourceAsStream(priKeyPath);
            if (inputStream != null) {
                byte[] buffer = new byte[1024];
                int len;
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                while ((len = inputStream.read(buffer)) != -1) {
                    bos.write(buffer, 0, len);
                }
                bos.close();
                byte[] array = bos.toByteArray();
                log.info("获取公钥和私钥 read={}", array.length);
                if (array.length > 0) {
                    this.privateKey = RsaUtils.getPrivateKey(array);
                } else {
                    throw new RuntimeException("JwtProperties init error");
                }
                inputStream.close();
            } else {
                throw new RuntimeException("JwtProperties init error");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("初始化公钥和私钥失败！", e);
            throw new RuntimeException();
        }
    }
}

